home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2007 September / PCWSEP07.iso / Software / Linux / Linux Mint 3.0 Light / LinuxMint-3.0-Light.iso / casper / filesystem.squashfs / usr / lib / sunbird / js / calTodo.js < prev    next >
Encoding:
JavaScript  |  2007-05-23  |  9.8 KB  |  312 lines

  1. /* -*- Mode: javascript; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
  2. /* ***** BEGIN LICENSE BLOCK *****
  3.  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
  4.  *
  5.  * The contents of this file are subject to the Mozilla Public License Version
  6.  * 1.1 (the "License"); you may not use this file except in compliance with
  7.  * the License. You may obtain a copy of the License at
  8.  * http://www.mozilla.org/MPL/
  9.  *
  10.  * Software distributed under the License is distributed on an "AS IS" basis,
  11.  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  12.  * for the specific language governing rights and limitations under the
  13.  * License.
  14.  *
  15.  * The Original Code is Oracle Corporation code.
  16.  *
  17.  * The Initial Developer of the Original Code is
  18.  *  Oracle Corporation
  19.  * Portions created by the Initial Developer are Copyright (C) 2004
  20.  * the Initial Developer. All Rights Reserved.
  21.  *
  22.  * Contributor(s):
  23.  *   Vladimir Vukicevic <vladimir.vukicevic@oracle.com>
  24.  *
  25.  * Alternatively, the contents of this file may be used under the terms of
  26.  * either the GNU General Public License Version 2 or later (the "GPL"), or
  27.  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
  28.  * in which case the provisions of the GPL or the LGPL are applicable instead
  29.  * of those above. If you wish to allow use of your version of this file only
  30.  * under the terms of either the GPL or the LGPL, and not to allow others to
  31.  * use your version of this file under the terms of the MPL, indicate your
  32.  * decision by deleting the provisions above and replace them with the notice
  33.  * and other provisions required by the GPL or the LGPL. If you do not delete
  34.  * the provisions above, a recipient may use your version of this file under
  35.  * the terms of any one of the MPL, the GPL or the LGPL.
  36.  *
  37.  * ***** END LICENSE BLOCK ***** */
  38.  
  39. //
  40. // calTodo.js
  41. //
  42.  
  43. //
  44. // constructor
  45. //
  46. function calTodo() {
  47.     this.wrappedJSObject = this;
  48.     this.initItemBase();
  49.     this.initTodo();
  50.  
  51.     this.todoPromotedProps = {
  52.         "DTSTART": true,
  53.         "DTEND": true,
  54.         "DTSTAMP": true,
  55.         "DUE": true,
  56.         "COMPLETED": true,
  57.         __proto__: this.itemBasePromotedProps
  58.     };
  59. }
  60.  
  61. // var trickery to suppress lib-as-component errors from loader
  62. var calItemBase;
  63.  
  64. var calTodoClassInfo = {
  65.     getInterfaces: function (count) {
  66.         var ifaces = [
  67.             Components.interfaces.nsISupports,
  68.             Components.interfaces.calIItemBase,
  69.             Components.interfaces.calITodo,
  70.             Components.interfaces.calIInternalShallowCopy,
  71.             Components.interfaces.nsIClassInfo
  72.         ];
  73.         count.value = ifaces.length;
  74.         return ifaces;
  75.     },
  76.  
  77.     getHelperForLanguage: function (language) {
  78.         return null;
  79.     },
  80.  
  81.     contractID: "@mozilla.org/calendar/todo;1",
  82.     classDescription: "Calendar Todo",
  83.     classID: Components.ID("{7af51168-6abe-4a31-984d-6f8a3989212d}"),
  84.     implementationLanguage: Components.interfaces.nsIProgrammingLanguage.JAVASCRIPT,
  85.     flags: 0
  86. };
  87.  
  88. calTodo.prototype = {
  89.     __proto__: calItemBase ? (new calItemBase()) : {},
  90.  
  91.     QueryInterface: function (aIID) {
  92.         if (aIID.equals(Components.interfaces.calITodo) ||
  93.             aIID.equals(Components.interfaces.calIInternalShallowCopy))
  94.             return this;
  95.  
  96.         if (aIID.equals(Components.interfaces.nsIClassInfo))
  97.             return calTodoClassInfo;
  98.  
  99.         return this.__proto__.__proto__.QueryInterface.call(this, aIID);
  100.     },
  101.  
  102.     initTodo: function () {
  103.         // todos by default don't have any of the dates set, or status, or
  104.         // percentComplete
  105.     },
  106.  
  107.     cloneShallow: function (aNewParent) {
  108.         var m = new calTodo();
  109.         this.cloneItemBaseInto(m, aNewParent);
  110.  
  111.         return m;
  112.     },
  113.  
  114.     clone: function () {
  115.         var m;
  116.  
  117.         if (this.parentItem != this) {
  118.             var clonedParent = this.mParentItem.clone();
  119.             m = clonedParent.recurrenceInfo.getExceptionFor (this.recurrenceId, true);
  120.         } else {
  121.             m = this.cloneShallow(null);
  122.         }
  123.  
  124.         return m;
  125.     },
  126.  
  127.     createProxy: function () {
  128.         if (this.mIsProxy) {
  129.             calDebug("Tried to create a proxy for an existing proxy!\n");
  130.             throw Components.results.NS_ERROR_UNEXPECTED;
  131.         }
  132.  
  133.         var m = new calTodo();
  134.         m.initializeProxy(this);
  135.  
  136.         return m;
  137.     },
  138.  
  139.     makeImmutable: function () {
  140.         this.makeItemBaseImmutable();
  141.     },
  142.  
  143.     get isCompleted() {
  144.         return this.completedDate != null ||
  145.                this.percentComplete == 100 ||
  146.                this.status == "COMPLETED";
  147.     },
  148.     
  149.     set isCompleted(v) {
  150.         if (v) {
  151.             if (!this.completedDate)
  152.                 this.completedDate = NewCalDateTime(new Date);
  153.             this.status = "COMPLETED";
  154.             this.percentComplete = 100;
  155.         } else {
  156.             this.deleteProperty("COMPLETED");
  157.             this.deleteProperty("STATUS");
  158.             this.deleteProperty("PERCENT-COMPLETE");
  159.         }
  160.     },
  161.  
  162.     get duration() {
  163.         if (!this.entryDate)
  164.             return null;
  165.         if (!this.dueDate)
  166.             return null;
  167.         return this.dueDate.subtractDate(this.entryDate);
  168.     },
  169.  
  170.     get recurrenceStartDate() {
  171.         return this.entryDate;
  172.     },
  173.  
  174.     icsEventPropMap: [
  175.     { cal: "DTSTART", ics: "startTime" },
  176.     { cal: "DUE", ics: "dueTime" },
  177.     { cal: "COMPLETED", ics: "completedTime" }],
  178.  
  179.     set icalString(value) {
  180.         this.icalComponent = icalFromString(value);
  181.     },
  182.  
  183.     get icalString() {
  184.         const icssvc = Components.
  185.           classes["@mozilla.org/calendar/ics-service;1"].
  186.           getService(Components.interfaces.calIICSService);
  187.         var calcomp = icssvc.createIcalComponent("VCALENDAR");
  188.         calcomp.prodid = "-//Mozilla Calendar//NONSGML Sunbird//EN";
  189.         calcomp.version = "2.0";
  190.         calcomp.addSubcomponent(this.icalComponent);
  191.         return calcomp.serializeToICS();
  192.     },
  193.  
  194.     get icalComponent() {
  195.         const icssvc = Components.
  196.           classes["@mozilla.org/calendar/ics-service;1"].
  197.           getService(Components.interfaces.calIICSService);
  198.         var icalcomp = icssvc.createIcalComponent("VTODO");
  199.         this.fillIcalComponentFromBase(icalcomp);
  200.         this.mapPropsToICS(icalcomp, this.icsEventPropMap);
  201.  
  202.         var bagenum = this.mProperties.enumerator;
  203.         while (bagenum.hasMoreElements()) {
  204.             var iprop = bagenum.getNext().
  205.                 QueryInterface(Components.interfaces.nsIProperty);
  206.             try {
  207.                 if (!this.todoPromotedProps[iprop.name]) {
  208.                     var icalprop = icssvc.createIcalProperty(iprop.name);
  209.                     icalprop.value = iprop.value;
  210.                     var propBucket = this.mPropertyParams[iprop.name]
  211.                     if (propBucket) {
  212.                         for (paramName in propBucket) {
  213.                             icalprop.setParameter(paramName,
  214.                                                   propBucket[paramName]);
  215.                         }
  216.                     }
  217.                     icalcomp.addProperty(icalprop);
  218.                 }
  219.             } catch (e) {
  220.                 // dump("failed to set " + iprop.name + " to " + iprop.value +
  221.                 // ": " + e + "\n");
  222.             }
  223.         }
  224.         return icalcomp;
  225.     },
  226.  
  227.     todoPromotedProps: null,
  228.  
  229.     set icalComponent(todo) {
  230.         this.modify();
  231.         if (todo.componentType != "VTODO") {
  232.             todo = todo.getFirstSubcomponent("VTODO");
  233.             if (!todo)
  234.                 throw Components.results.NS_ERROR_INVALID_ARG;
  235.         }
  236.  
  237.         this.setItemBaseFromICS(todo);
  238.         this.mapPropsFromICS(todo, this.icsEventPropMap);
  239.         this.mIsAllDay = this.mStartDate && this.mStartDate.isDate;
  240.  
  241.         this.importUnpromotedProperties(todo, this.todoPromotedProps);
  242.         // Importing didn't really change anything
  243.         this.mDirty = false;
  244.     },
  245.  
  246.     isPropertyPromoted: function (name) {
  247.         return (this.todoPromotedProps[name]);
  248.     },
  249.  
  250.     getOccurrencesBetween: function(aStartDate, aEndDate, aCount) {
  251.         if (this.recurrenceInfo) {
  252.             return this.recurrenceInfo.getOccurrences(aStartDate, aEndDate, 0, aCount);
  253.         }
  254.         if (!this.entryDate && !this.dueDate)
  255.             return null;
  256.  
  257.         var entry = ensureDateTime(this.entryDate);
  258.         var due = ensureDateTime(this.dueDate);
  259.  
  260.         var queryStart = ensureDateTime(aStartDate);
  261.         var queryEnd = ensureDateTime(aEndDate);
  262.  
  263.         var isInterval = entry && due;
  264.         var isZeroLength = isInterval ? !entry.compare(due) : true;
  265.         var dateTime = entry ? entry : due;
  266.         
  267.         if ((isZeroLength &&
  268.              dateTime.compare(queryStart) >= 0 &&
  269.              dateTime.compare(queryEnd) < 0) ||
  270.             (!isZeroLength &&
  271.              entry.compare(queryEnd) < 0 &&
  272.              due.compare(queryStart) > 0)) {
  273.             
  274.           aCount.value = 1;
  275.           return ([ this ]);
  276.         }
  277.  
  278.         aCount.value = 0;
  279.         return null;
  280.     },
  281.  
  282.     set entryDate(value) {
  283.         this.modify();
  284.         
  285.         // We're about to change the start date of an item which probably
  286.         // could break the associated calIRecurrenceInfo. We're calling
  287.         // the appropriate method here to adjust the internal structure in
  288.         // order to free clients from worrying about such details.
  289.         if (this.parentItem == this) {
  290.             var rec = this.recurrenceInfo;
  291.             if (rec) {
  292.                 rec.onStartDateChange(value,this.entryDate);
  293.             }
  294.         }
  295.  
  296.         this.setProperty("DTSTART", value);
  297.     },
  298.  
  299.     get entryDate() {
  300.         return this.getProperty("DTSTART");
  301.     }
  302. };
  303.         
  304. // var decl to prevent spurious error messages when loaded as component
  305.  
  306. var makeMemberAttr;
  307. if (makeMemberAttr) {
  308.     makeMemberAttr(calTodo, "DUE", null, "dueDate", true);
  309.     makeMemberAttr(calTodo, "COMPLETED", null, "completedDate", true);
  310.     makeMemberAttr(calTodo, "PERCENT-COMPLETE", 0, "percentComplete", true);
  311. }
  312.